package com.digua.base.socket.response

import com.digua.base.socket.request.Request


class ErrorResponse {

    companion object {
        /**
         * ws 未链接
         */
        const val ERROR_NO_CONNECT = 0

        /**
         * 未知错误
         */
        const val ERROR_UNKNOWN = 1

        /**
         * 初始化未完成
         */
        const val ERROR_UN_INIT = 2
    }

    /**
     * 错误信息
     */
    var errorCode = 0

    /**
     * 错误原因
     */
    var cause: Throwable? = null

    /**
     * 发送的数据，可能为空
     */
    var requestData: Request<*>? = null

    /**
     * 响应的数据，可能为空
     */
    var responseData: Response<*>? = null

    /**
     * 错误描述，客户端可以通过这个字段来设置统一的错误提示等等
     */
    var description: String? = null

    /**
     * 保留字段，可以自定义存放任意数据
     */
    var reserved: Any? = null

    /**
     * 初始化
     */
    fun init(request: Request<*>?, code: Int, tr: Throwable?) {
        requestData = request
        cause = tr
        errorCode = code
    }

    fun release() {
        ResponseFactory.releaseErrorResponse(this)
    }

    override fun toString() =
        "ErrorResponse(ERROR_NO_CONNECT=$ERROR_NO_CONNECT, ERROR_UNKNOWN=$ERROR_UNKNOWN, ERROR_UN_INIT=$ERROR_UN_INIT, errorCode=$errorCode, cause=$cause, " + "requestData=$requestData, responseData=$responseData, description=$description, reserved=$reserved)"


}